<?php

class ProductGroupTable extends PriorityNestedTable
{
          public static function getInstance() {
            return Doctrine::getTable('ProductGroup');
        }
  
	public function findAll( $hydrationMode = null ){
		$q = Doctrine::getTable('ProductGroup')->createQuery('prodgroup')
				->from('ProductGroup pg');
				
		return $this->addDefaultSort( $q )->execute();
	}
        /*
         * Groups are acting like products and we using only lvl 1
         */
	public function findSaleshouseGroups( $limit = false, $param = false ){
		$q = Doctrine::getTable('ProductGroup')->createQuery('prodgroup')
                                ->select('pg.*, p.*, pgt.*, pt.*, pr.* ')
				->from('ProductGroup pg')
                                ->innerJoin('pg.Translation pgt WITH pgt.lang = ?', sfContext::getInstance()->getUser()->getCulture())
                                ->innerJoin('pg.Products pr')
                                ->innerJoin('pr.Product p')
                                ->innerJoin('p.Translation pt WITH pt.lang = ?', sfContext::getInstance()->getUser()->getCulture())
                                ->where('pg.level = 1')
                                ->addWhere('p.is_active = 1');
                                if($limit)
                                    $q->limit($limit);
                                if($param)
                                    $q->addWhere('pg.parameter = ?', $param);
				
		return $this->addDefaultSort( $q )->execute();
	}
	
        
        
        
	
	public function findAllQuery(){
		return Doctrine::getTable('ProductGroup')->createQuery('prodgroup')
				->from('ProductGroup pg');		
	}
	
	public function addNoRootContstraint( Doctrine_Query $q ){
		return $q->andWhere($q->getRootAlias().'.level > ?' , 0 );
	}
	
	public function addFrontendViewConstraints( Doctrine_Query $q, $culture ){
		return $this->addNoRootContstraint($q)
				->leftJoin("c.Translation t WITH t.lang = ? AND t.name != ''" , $culture);
				//->innerJoin('c.Translation t WITH t.lang = ?', $culture);
	}
	
	public function getActiveProductGroups(){
		return $this->findAll();	
	}
	
	
	public function addDefaultSort(Doctrine_Query $q){
		return $q->orderBy( $q->getRootAlias(). '.lft ASC');
	}
	
	public function getRoot(){
		$roots = $this->getTree()->fetchRoots();
		return $roots[0];
	}
	
	public function findI18nSlug($slug){
		return $this->createQuery()
				->from('ProductGroup p')
				->innerJoin('p.Translation t WITH t.slug = ?', $slug)
				->execute();
	}

}